<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `addresscompanymap` (
	`addressid` int(11) NOT NULL,
	`companyid` int(11) NOT NULL,INDEX(`addressid`, `companyid`)) ENGINE=MyISAM;
*/

/**
* <b>AddressCompanyMap</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @copyright Free for personal & commercial use. (Offered under the BSD license)
*/
class AddressCompanyMap
{
	public $addressId = '';

	public $companyId = '';

	public $pog_attribute_type = array(
		"addressId" => array('db_attributes' => array("NUMERIC", "INT")),
		"companyId" => array('db_attributes' => array("NUMERIC", "INT")));
		public $pog_query;
	
	
	/**
	* Creates a mapping between the two objects
	* @param Address $object 
	* @param Company $otherObject 
	* @return 
	*/
	function AddMapping($object, $otherObject)
	{
		if ($object instanceof Address && $object->addressId != '')
		{
			$this->addressId = $object->addressId;
			$this->companyId = $otherObject->companyId;
			return $this->Save();
		}
		else if ($object instanceof Company && $object->companyId != '')
		{
			$this->companyId = $object->companyId;
			$this->addressId = $otherObject->addressId;
			return $this->Save();
		}
		else
		{
			return false;
		}
	}
	
	
	/**
	* Removes the mapping between the two objects
	* @param Object $object 
	* @param Object $object2 
	* @return 
	*/
	function RemoveMapping($object, $otherObject = null)
	{
		$connection = Database::Connect();
		if ($object instanceof Address)
		{
			$this->pog_query = "delete from `addresscompanymap` where `addressid` = '".$object->addressId."'";
			if ($otherObject != null && $otherObject instanceof Company)
			{
				$this->pog_query .= " and `companyid` = '".$otherObject->companyId."'";
			}
		}
		else if ($object instanceof Company)
		{
			$this->pog_query = "delete from `addresscompanymap` where `companyid` = '".$object->companyId."'";
			if ($otherObject != null && $otherObject instanceof Address)
			{
				$this->pog_query .= " and `addressid` = '".$otherObject->addressId."'";
			}
		}
		Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Physically saves the mapping to the database
	* @return 
	*/
	function Save()
	{
		$connection = Database::Connect();
		$this->pog_query = "select `addressid` from `addresscompanymap` where `addressid`='".$this->addressId."' AND `companyid`='".$this->companyId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows == 0)
		{
			$this->pog_query = "insert into `addresscompanymap` (`addressid`, `companyid`) values ('".$this->addressId."', '".$this->companyId."')";
		}
		return Database::InsertOrUpdate($this->pog_query, $connection);
	}
}
?>